﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConnectionLayer;
using System.Data.SqlClient;
using System.IO;
using System.Xml;
using System.Windows.Forms;

namespace BusinessLayer
{
    public static class OficinaActions
    {
        public static bool CriarOficina(String NIFOfic, String NomeOfic, String Fax, String TelefoneOfic, String MoradaOfic)
        {
            ConnectionFactory con = new ConnectionFactory();

            try
            {
                con.GetConnection.Open();
                SqlCommand cmd = con.GetConnection.CreateCommand();
                cmd.CommandText = "insert into Oficina (nifOfic,nomeOfic,moradaOfic,telefoneOfic,faxOfic) values (@NIFOfic,@NomeOfic,@MoradaOfic,@TelefoneOfic,@Fax)";
                cmd.Parameters.AddWithValue("@NIFOfic", NIFOfic);
                cmd.Parameters.AddWithValue("@NomeOfic", NomeOfic);
                cmd.Parameters.AddWithValue("@MoradaOfic", MoradaOfic);
                cmd.Parameters.AddWithValue("@TelefoneOfic", TelefoneOfic);
                cmd.Parameters.AddWithValue("@Fax", Fax);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception e) { MessageBox.Show(e.Message); return false; }
            finally
            {
                try
                {
                    con.GetConnection.Close();
                }
                catch (Exception e) { MessageBox.Show(e.Message); }
            }
        }
        public static bool AlterarOficina(String Telefone, String Fax, String Morada)
        {
            ConnectionFactory con = new ConnectionFactory();

            try
            {
                con.GetConnection.Open();
                SqlCommand cmd = con.GetConnection.CreateCommand();
                cmd.CommandText = "update Oficina set moradaOfic = isnull(@Morada,moradaOfic),telefoneOfic = isnull(@Telefone,telefoneOfic),faxOfic = isnull(@Fax,faxOfic) where codOfic = @idOfic";
                cmd.Parameters.AddWithValue("@Morada", Morada);
                cmd.Parameters.AddWithValue("@Telefone", Telefone);
                cmd.Parameters.AddWithValue("@Fax", Fax);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception e) { MessageBox.Show(e.Message); return false; }

            finally
            {
                try
                {
                    con.GetConnection.Close();
                }
                catch (SqlException e) { MessageBox.Show(e.Message); }
            }
        }
        public static String GetXml(){ 
            ConnectionFactory con = new ConnectionFactory();

            try
            {
                con.GetConnection.Open();
                SqlCommand cmd = con.GetConnection.CreateCommand();
                cmd.CommandText = "SP_TrasnformToHtml";
                SqlParameter param = new SqlParameter("@val", System.Data.SqlDbType.Xml);
                param.Direction = System.Data.ParameterDirection.Output;
                cmd.Parameters.Add(param);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.ExecuteNonQuery();
                String xml = null;
                if (param.Value is String)
                    xml = (String) param.Value;
                return xml;
            }
            catch (Exception e) { MessageBox.Show(e.Message); return null; }

            finally
            {
                try
                {
                    con.GetConnection.Close();
                }
                catch (SqlException e) { MessageBox.Show(e.Message); }
            }

        
        }

        public static String criaFicheiroXMl(String dados) { 
        
            String fileTemp = "fileTemp.xml"; 
            using (StreamWriter st = new StreamWriter(fileTemp)) {
                try{
                st.WriteLine(dados);
                }
                catch(Exception e){MessageBox.Show(e.Message);}
            }
           
            return fileTemp;
        }

    }
}
